{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "94edb6dd-a329-49e9-b2e1-bb9b81c14a0a",
   "metadata": {},
   "source": [
    "### I. Install the Kaggle API\n",
    "- register the Kaggle website by using the website (https://www.kaggle.com/)\n",
    "- install the kaggle API by using （https://www.kaggle.com/docs/api）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "17f1c15e-e444-46b4-9d7e-23b906fc19a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "#!pip install kaggle"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29af8589-6eaa-4495-9bfc-ccf2e0512edb",
   "metadata": {},
   "source": [
    "### II. Use Kaggle API "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6884bab4-8a7c-457d-8063-8de689d6923d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "usage: kaggle [-h] [-v]\n",
      "              {competitions,c,datasets,d,kernels,k,models,m,files,f,config}\n",
      "              ...\n",
      "\n",
      "options:\n",
      "  -h, --help            show this help message and exit\n",
      "  -v, --version         show program's version number and exit\n",
      "\n",
      "commands:\n",
      "  {competitions,c,datasets,d,kernels,k,models,m,files,f,config}\n",
      "                        Use one of:\n",
      "                        competitions {list, files, download, submit, submissions, leaderboard}\n",
      "                        datasets {list, files, download, create, version, init, metadata, status}\n",
      "                        kernels {list, init, push, pull, output, status}\n",
      "                        config {view, set, unset}\n",
      "    competitions (c)    Commands related to Kaggle competitions\n",
      "    datasets (d)        Commands related to Kaggle datasets\n",
      "    kernels (k)         Commands related to Kaggle kernels\n",
      "    files (f)           Commands related files\n",
      "    config              Configuration settings\n"
     ]
    }
   ],
   "source": [
    "!kaggle -h"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4feb012f-cd06-4f7e-be4a-f3808ec0f6a0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ref                                                         title                                               size  lastUpdated          downloadCount  voteCount  usabilityRating  \n",
      "----------------------------------------------------------  -------------------------------------------------  -----  -------------------  -------------  ---------  ---------------  \n",
      "tunguz/used-car-auction-prices                              Used Car Auction Prices                             19MB  2021-05-18 17:49:43           5735        104  1.0              \n",
      "doaaalsenani/usa-cers-dataset                               US Cars Dataset                                     66KB  2020-04-22 14:38:05          30818        356  1.0              \n",
      "dansbecker/melbourne-housing-snapshot                       Melbourne Housing Snapshot                         451KB  2018-06-05 12:52:24         130934       1395  0.7058824        \n",
      "bogof666/shanghai-car-license-plate-auction-price           Shanghai Car License Plate Auction Price             3KB  2019-01-24 03:18:01           1921         59  0.7058824        \n",
      "anthonypino/melbourne-housing-market                        Melbourne Housing Market                             2MB  2018-10-14 23:33:37          52482        700  0.7352941        \n",
      "danielkyrka/bmw-pricing-challenge                           BMW Pricing Challenge                               74KB  2019-03-07 17:01:02           3460         38  0.7647059        \n",
      "ulrikthygepedersen/car-kick                                 Car Kick                                             3MB  2023-02-01 13:53:29            461         16  1.0              \n",
      "asimzahid/pakistans-largest-pakwheels-automobiles-listings  Pakistan's Largest PakWheels Automobiles Listings   11MB  2021-10-23 12:10:54            486         19  1.0              \n",
      "zeesolver/melbourne-housing-model                           Prediction New Housing Price Model                 614KB  2022-12-25 17:28:41            151          5  0.9705882        \n",
      "dhirajbembade/melbourne-housing-snapshot                     Melbourne Housing Snapshot                        451KB  2023-06-05 07:10:55             51         13  0.5294118        \n",
      "maciekog/car-purchase-decision-making-dataset               Car purchase decision making dataset                14KB  2023-05-25 18:32:59             54          0  0.5294118        \n"
     ]
    }
   ],
   "source": [
    "!kaggle datasets list -s \"car auction\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e48c7407-6f26-475e-846c-6d350a2b2555",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading used-car-auction-prices.zip to /mnt/c/Research/Teaching/A_Classes/20231119_QuantEcon_Prof/A_Git/codes\n",
      "100%|██████████████████████████████████████| 19.0M/19.0M [00:04<00:00, 6.93MB/s]\n",
      "100%|██████████████████████████████████████| 19.0M/19.0M [00:04<00:00, 4.72MB/s]\n"
     ]
    }
   ],
   "source": [
    "!kaggle datasets download tunguz/used-car-auction-prices"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55d5da9c-a814-4a2a-9756-1c52942cea76",
   "metadata": {},
   "source": [
    "### III. unzip the data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5120555a-a73a-4e49-993a-31690d69c528",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "filename='used-car-auction-prices.zip'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2bcd4371-b709-49f0-bcb3-068469b29784",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".ipynb_checkpoints\n",
      "Class2a_Kaggle_API.ipynb\n",
      "Class2b_Data_Analysis.ipynb\n",
      "Class2c_Selenium.ipynb\n",
      "Class3_GPA_Income.ipynb\n",
      "Homework_Car_Auction.ipynb\n",
      "\n",
      "\n",
      "\n"
     ]
    },
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'used-car-auction-prices.zip'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_3180\\3089367016.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      4\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mitem\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"\\n\\n\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[1;32mwith\u001b[0m \u001b[0mzipfile\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mZipFile\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfilename\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"r\"\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mzf\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      7\u001b[0m     \u001b[0mzf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mextractall\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\zipfile.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, file, mode, compression, allowZip64, compresslevel, strict_timestamps)\u001b[0m\n\u001b[0;32m   1246\u001b[0m             \u001b[1;32mwhile\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1247\u001b[0m                 \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1248\u001b[1;33m                     \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfp\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mio\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfile\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfilemode\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1249\u001b[0m                 \u001b[1;32mexcept\u001b[0m \u001b[0mOSError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1250\u001b[0m                     \u001b[1;32mif\u001b[0m \u001b[0mfilemode\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mmodeDict\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'used-car-auction-prices.zip'"
     ]
    }
   ],
   "source": [
    "import zipfile\n",
    "import os\n",
    "for item in os.listdir(path='.'):\n",
    "    print(item)\n",
    "print(\"\\n\\n\")    \n",
    "with zipfile.ZipFile(filename,\"r\") as zf:\n",
    "    zf.extractall()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "017819e8-704f-46d9-88c4-d57382ce4a08",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
